package com.winbomb.simulife.impl.operations;

import com.winbomb.simulife.HonorType;
import com.winbomb.simulife.World;
import com.winbomb.simulife.base.IChoice;
import com.winbomb.simulife.framework.Game;
import com.winbomb.simulife.framework.Pixmap;
import com.winbomb.simulife.game.screens.YPElectionScreen;
import com.winbomb.simulife.impl.BaseChoice;
import com.winbomb.simulife.impl.BaseOperation;
import com.winbomb.simulife.impl.choices.CancelChoice;
import com.winbomb.simulife.util.ProbUtil;

/**
 * YP = Young Pioneer 少先队
 * 
 * 少先队竞选的操作类
 * 
 * @author winbomb
 * 
 */
public class YPElectionOpt extends BaseOperation {

	private HonorType targetHonor;

	public YPElectionOpt(World world) {
		super(world);

		if (!master.hasHonor(HonorType.HN_YP_APPLY)) {
			targetHonor = HonorType.HN_YP_APPLY;
		} else if (!master.hasHonor(HonorType.HN_YP_JUNIOR)) {
			targetHonor = HonorType.HN_YP_JUNIOR;
		} else if (!master.hasHonor(HonorType.HN_YP_SECONDARY)) {
			targetHonor = HonorType.HN_YP_SECONDARY;
		} else if (!master.hasHonor(HonorType.HN_YP_SENIOR)) {
			targetHonor = HonorType.HN_YP_SENIOR;
		} else {
			targetHonor = HonorType.HN_YP_APPLY;
		}
	}

	@Override
	public Pixmap getIcon() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		String name = "";
		if (targetHonor == HonorType.HN_YP_APPLY) {
			name = "入队";
		} else if (targetHonor == HonorType.HN_YP_JUNIOR) {
			name = "小队长";
		} else if (targetHonor == HonorType.HN_YP_SECONDARY) {
			name = "中队长";
		} else if (targetHonor == HonorType.HN_YP_SENIOR) {
			name = "大队长";
		}

		return name;
	}

	@Override
	public int getTimeCost() {
		return 2;
	}

	@Override
	public Pixmap getAnimation() {
		return null;
	}

	@Override
	public IChoice[] getChoices() {
		IChoice[] choices = new IChoice[2];
		choices[0] = new ElectionChoice("参加竞选");
		choices[1] = new CancelChoice("放弃竞选");

		return choices;
	}

	@Override
	public String getDescription() {
		return "竞选";
	}

	@Override
	public boolean isEnable() {
		return (master.getIdentity().isPupil() && !master.hasHonor(HonorType.HN_YP_SENIOR));
	}

	private boolean isApplyAccepted(HonorType honor) {
		boolean bAccepted = false;
		if (honor == HonorType.HN_YP_APPLY) {
			bAccepted = ProbUtil.test(1.0f);
		} else if (honor == HonorType.HN_YP_JUNIOR) {
			bAccepted = ProbUtil.test(0.7f);
		} else if (honor == HonorType.HN_YP_SECONDARY) {
			bAccepted = ProbUtil.test(0.3f);
		} else if (honor == HonorType.HN_YP_SENIOR) {
			bAccepted = ProbUtil.test(0.1f);
		}

		return bAccepted;
	}

	private HonorType getNextHonor(HonorType honor) {
		if (honor == HonorType.HN_YP_APPLY) {
			return HonorType.HN_YP_JUNIOR;
		} else if (honor == HonorType.HN_YP_JUNIOR) {
			return HonorType.HN_YP_SECONDARY;
		} else if (honor == HonorType.HN_YP_SECONDARY) {
			return HonorType.HN_YP_SENIOR;
		} else {
			return HonorType.HN_YP_SENIOR;
		}

	}

	class ElectionChoice extends BaseChoice {

		public ElectionChoice(String name) {
			super(name);
		}

		@Override
		public void doChoice(Game game) {
			boolean bSucc = isApplyAccepted(targetHonor);
			game.setScreen(new YPElectionScreen(game, bSucc, targetHonor));

			if (bSucc) {
				master.addHonor(targetHonor);
				master.addAcheivment(targetHonor.getHonorPoint());
				targetHonor = getNextHonor(targetHonor);
			}
		}
	}
}
